package db_sports

import (
	"context"
	"github.com/olivere/elastic/v7"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/timehelper"
	"longmen/server/pkg/db/mysql/models/models_sports"
	"reflect"
)

//55直播赛事列表
/**sport_id 1.足球 2.篮球 3.热门 4.全部
matchStateId 1.进行中 2.赛程  3.赛果*/
func Get55MatchEsList(db *elastic.Client, sportId int, comp string, matchStateId int, matchDate int64, platform int32) []*api_models.EsMatchTempSchedule {
	limitNum := 8
	if sportId == 4 && matchStateId == 1 {
		limitNum = 4
	}
	if platform == 1 {
		limitNum = limitNum * 2
	}
	ascending := true
	var data []*api_models.EsMatchTempSchedule
	filter1 := elastic.NewBoolQuery() //进行中
	filter2 := elastic.NewBoolQuery() //未开赛
	filter3 := elastic.NewBoolQuery() //赛果
	var filter1Bool, filter2Bool, filter3Bool bool
	//params["match_time"] = map[string]interface{}{">=": startTime, "<=": endTime}
	if sportId == 4 { //全部
		if matchStateId == MatchStaring {
			filter1.Filter(elastic.NewTermsQuery("match_status", MatchStaring))
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending))
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished))
			filter1Bool = true
			filter2Bool = true
			filter3Bool = true
			ascending = false
		} else if matchStateId == MatchPending {
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending))
			filter2Bool = true
		} else if matchStateId == MatchFinished {
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished))
			filter3Bool = true
		}
	} else if sportId == 3 { //热门
		if matchStateId == MatchStaring {
			filter1.Filter(elastic.NewTermsQuery("match_status", MatchStaring), elastic.NewTermsQuery("flag", 2))
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("flag", 2))
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("flag", 2))
			filter1Bool = true
			filter2Bool = true
			filter3Bool = true
			ascending = false
		} else if matchStateId == MatchPending {
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("flag", 2))
			filter2Bool = true
		} else if matchStateId == MatchFinished {
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("flag", 2))
			filter3Bool = true
		}
	} else if sportId == 2 { //篮球
		if matchStateId == MatchStaring {
			filter1.Filter(elastic.NewTermsQuery("match_status", MatchStaring), elastic.NewTermsQuery("sport_id", BasketBallSportId))
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("sport_id", BasketBallSportId))
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("sport_id", BasketBallSportId))
			filter1Bool = true
			filter2Bool = true
			filter3Bool = true
			ascending = false
		} else if matchStateId == MatchPending {
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("sport_id", BasketBallSportId))
			filter2Bool = true
		} else if matchStateId == MatchFinished {
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("sport_id", BasketBallSportId))
			filter3Bool = true
		}
	} else if sportId == 1 { //足球
		if matchStateId == MatchStaring {
			filter1.Filter(elastic.NewTermsQuery("match_status", MatchStaring), elastic.NewTermsQuery("sport_id", FootBallSportId))
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("sport_id", FootBallSportId))
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("sport_id", FootBallSportId))
			filter1Bool = true
			filter2Bool = true
			filter3Bool = true
			ascending = false
		} else if matchStateId == MatchPending {
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("sport_id", FootBallSportId))
			filter2Bool = true
		} else if matchStateId == MatchFinished {
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("sport_id", FootBallSportId))
			filter3Bool = true
		}
	} else if len(comp) > 1 {
		if matchStateId == MatchStaring {
			filter1.Filter(elastic.NewTermsQuery("match_status", MatchStaring), elastic.NewTermsQuery("comp_short_zh.keyword", comp))
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("comp_short_zh.keyword", comp))
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("comp_short_zh.keyword", comp))
			filter1Bool = true
			filter2Bool = true
			filter3Bool = true
			ascending = false
		} else if matchStateId == MatchPending {
			filter2.Filter(elastic.NewTermsQuery("match_status", MatchPending), elastic.NewTermsQuery("comp_short_zh.keyword", comp))
			filter2Bool = true
		} else if matchStateId == MatchFinished {
			filter3.Filter(elastic.NewTermsQuery("match_status", MatchFinished), elastic.NewTermsQuery("comp_short_zh.keyword", comp))
			filter3Bool = true
		}
	}
	//global.Log.Debugf("rs1Where:%v,rs2Where:%v,rs3Where:%v", filter1Bool, filter2Bool, filter3Bool)

	dataResp := func(filter *elastic.BoolQuery, ascending bool, matchDate int64) []*api_models.EsMatchTempSchedule {
		matchEndDate := matchDate + 86400
		query := elastic.NewBoolQuery()
		boolQuery := query.Must(filter)
		if matchDate > 0 {
			rangeQuery := elastic.NewRangeQuery("match_time").Gte(matchDate).Lt(matchEndDate)
			boolQuery = query.Must(filter, rangeQuery)
		}
		rs := []*api_models.EsMatchTempSchedule{}
		res, _ := db.Search(models_sports.MatchTempSchedule{}.TableName()).
			From(0).
			Size(limitNum).
			Query(boolQuery).
			Sort("match_time", ascending).
			Do(context.Background())
		if res.TotalHits() > 0 {
			var itemSchedule *api_models.EsMatchTempSchedule
			// 通过Each方法，将es结果的json结构转换成struct对象
			for _, item := range res.Each(reflect.TypeOf(itemSchedule)) {
				if val, ok := item.(*api_models.EsMatchTempSchedule); ok {
					//global.Log.Debugf("val:%+v", val)
					rs = append(rs, val)
				}
			}
		}
		return rs
	}

	if filter1Bool {
		if matchDate < 1 {
			matchDate = timehelper.GetToday().Unix()
		}
		data = append(data, dataResp(filter1, true, matchDate)...)
		if sportId == 4 && matchStateId == 1 {
			if limitNum <= len(data) {
				return data
			}
		}
	}

	if filter2Bool {
		data = append(data, dataResp(filter2, ascending, matchDate)...)
		if sportId == 4 && matchStateId == 1 {
			if limitNum <= len(data) {
				return data
			}
		}
	}

	if filter3Bool {
		data = append(data, dataResp(filter3, ascending, matchDate)...)
	}

	return data
}

//SEO用
/**sport_id 1.足球 2.篮球 0.全部
matchStateId 1.进行中 2.赛程  3.赛果*/
func GetSeoMatchEsList(db *elastic.Client, sportId int, comp string, matchStateId int, matchDate int64) []*api_models.EsMatchTempSchedule {
	ascending := true
	var data []*api_models.EsMatchTempSchedule
	filter := elastic.NewBoolQuery()
	if sportId == 1 { //足球
		filter.Filter(elastic.NewTermsQuery("sport_id", FootBallSportId))
	} else if sportId == 2 {
		filter.Filter(elastic.NewTermsQuery("sport_id", BasketBallSportId))
	}
	if matchStateId == MatchStaring {
		filter.Filter(elastic.NewTermsQuery("match_status", MatchStaring))
		ascending = false
	} else if matchStateId == MatchPending {
		filter.Filter(elastic.NewTermsQuery("match_status", MatchPending))
	} else if matchStateId == MatchFinished {
		filter.Filter(elastic.NewTermsQuery("match_status", MatchFinished))
	}
	if comp != "" {
		filter.Filter(elastic.NewTermsQuery("comp_short_zh.keyword", comp))
	}

	dataResp := func(filter *elastic.BoolQuery, ascending bool, matchDate int64) []*api_models.EsMatchTempSchedule {
		matchEndDate := matchDate + 86400
		if matchDate > 0 {
			rangeQuery := elastic.NewRangeQuery("match_time").Gte(matchDate).Lt(matchEndDate)
			filter = filter.Filter(rangeQuery)
		}
		var rs []*api_models.EsMatchTempSchedule
		res, _ := db.Search(models_sports.MatchTempSchedule{}.TableName()).
			From(0).
			Size(100).
			Query(filter).
			Sort("match_time", ascending).
			Do(context.Background())
		if res.TotalHits() > 0 {
			var itemSchedule *api_models.EsMatchTempSchedule
			// 通过Each方法，将es结果的json结构转换成struct对象
			for _, item := range res.Each(reflect.TypeOf(itemSchedule)) {
				if val, ok := item.(*api_models.EsMatchTempSchedule); ok {
					//global.Log.Debugf("val:%+v", val)
					rs = append(rs, val)
				}
			}
		}
		return rs
	}
	data = append(data, dataResp(filter, ascending, matchDate)...)
	return data
}
